package net.onecloud.onecloud;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import net.onecloud.onecloud.account.AccountPage;
import net.onecloud.onecloud.base.BaseAppCompatActivity;
import net.onecloud.onecloud.base.BasePage;
import net.onecloud.onecloud.employ.EmployPage;
import net.onecloud.onecloud.home.HomePage;
import net.onecloud.onecloud.talk.TalkPage;
import net.onecloud.onecloud.utils.LogUtils;
import net.onecloud.onecloud.utils.T;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseAppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private RadioGroup mRadioGroup;
    private MyPagerAdapter myPagerAdapter;
    private List<BasePage> basePageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initData();

    }

    private void initData() {
        HideToolBar(true);
        basePageList = new ArrayList<>();
        HomePage homePage = new HomePage(this);
        EmployPage employPage = new EmployPage(this);
        TalkPage talkPage = new TalkPage(this);
        AccountPage accountPage = new AccountPage(this);
        basePageList.add(homePage);
        basePageList.add(employPage);
        basePageList.add(talkPage);
        basePageList.add(accountPage);
        myPagerAdapter.setItems(basePageList);
        basePageList.get(0).initData();//默认初始化第一个
        myPagerAdapter.notifyDataSetChanged();

    }

    private void initListener() {
        mRadioGroup.setOnCheckedChangeListener(this);
        mViewPager.addOnPageChangeListener(this);
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.m_viewpager);
        mRadioGroup = (RadioGroup) findViewById(R.id.m_radiogroup);
        myPagerAdapter = new MyPagerAdapter();
        mViewPager.setAdapter(myPagerAdapter);
    }

    private void setCurrentPage(int position) {
        basePageList.get(position).initData();
        mViewPager.setCurrentItem(position);


    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_home: {
                HideToolBar(true);
                setCurrentPage(0);
                break;
            }
            case R.id.rb_employ: {
                setToolBarTitle("招聘信息");
                HideToolBar(false);
                setCurrentPage(1);
                break;
            }
            case R.id.rb_campany: {
                setToolBarTitle("企业信息");
                HideToolBar(false);
                setCurrentPage(2);
                break;
            }
            case R.id.rb_account: {
                setToolBarTitle("我的门客");
                HideToolBar(false);
                setCurrentPage(3);
                break;
            }
            default: {
                break;
            }
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton radioButton = (RadioButton) mRadioGroup.getChildAt(position);
        radioButton.setChecked(true);


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    class MyPagerAdapter extends PagerAdapter {
        private List<BasePage> mBasePagerList;

        public void setItems(List<BasePage> list) {
            mBasePagerList = list;
        }

        @Override
        public int getCount() {
            return mBasePagerList == null ? 0 : mBasePagerList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            LogUtils.i("ContentFragment's instantiateItem method was called");
            if (mBasePagerList != null && mBasePagerList.size() > 0) {
                container.addView(mBasePagerList.get(position).mRootView);
                return mBasePagerList.get(position).mRootView;
            } else {
                TextView textView = new TextView(MainActivity.this);
                textView.setText("pager is null");
                textView.setGravity(Gravity.CENTER);
                container.addView(textView);
                return textView;
            }

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
            object = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        T.showShort(this, requestCode+"");
        switch (requestCode) {
            case 0: {
                break;
            }
            case 1: {
                break;
            }
            case 2: {
                break;
            }
            case 3: {
                basePageList.get(3).initData();
                break;
            }
            default: {
                break;
            }
        }
    }
}
